package com.chaochis.wind.service.impl;

import com.chaochis.wind.common_exceptions.not_exist_exception.UserNotExistException;
import com.chaochis.wind.enums.DataStatus;
import com.chaochis.wind.mapper.WindUserMapper;
import com.chaochis.wind.service.WindUserService;
import com.chaochis.wind.vo.WindAccount;
import com.chaochis.wind.vo.WindUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 刘超
 * @version 1.0.0
 * 该类是风旅用户表操作接口的实现类
 */
@Service("windUserService")
public class WindUserServiceImpl implements WindUserService {
  private WindUserMapper windUserMapper;
  
  @Autowired
  public void setWindUserMapper(WindUserMapper windUserMapper) {
    this.windUserMapper = windUserMapper;
  }
  
  private RedisTemplate<String, Object> redisTemplate;
  
  @Autowired
  public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
    this.redisTemplate = redisTemplate;
  }
  
  /**
   * 获取风旅用户列表
   *
   * @param windUser 要查询的风旅用户参数
   */
  @Override
  public List<WindUser> getWindUserList(WindUser windUser) {
    return this.windUserMapper.getWindUserList(windUser);
  }
  
  /**
   * 添加风旅用户
   *
   * @param windUser 要添加的风旅用户
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public void addWindUser(WindUser windUser) {
    this.windUserMapper.insert(windUser);
  }
  
  /**
   * 修改用户的基本信息
   *
   * @param windUser 要修改的用户信息
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public void updateWindUserInfo(WindUser windUser) {
    this.windUserMapper.updateById(windUser);
  }
  
  /**
   * 物理删除用户
   *
   * @param id 用户id
   */
  @Override
  public void deleteWindUser(String id) throws Exception {
    // 先从数据库里查询该用户的状态
    var findWindUser = this.windUserMapper.selectById(id);
    if (null == findWindUser) {
      throw new UserNotExistException("您要删除的该用户不存在，请重试!");
    }
    if (null == findWindUser.getUserStatus()) {
      throw new Exception("用户状态异常，请联系管理员!");
    }
    if (DataStatus.Normal == findWindUser.getUserStatus()) {
      // 用户状态是正常的话，需要进行逻辑删除
      var updateWindUser = new WindUser();
      updateWindUser.setUserStatus(DataStatus.Deleted);
      updateWindUser.setUserStatusText(DataStatus.Deleted.getText());
      updateWindUser.setUserId(id);
      this.windUserMapper.changeWindUserStatus(updateWindUser);
    } else {
      // 状态是删除或者未激活时直接物理删除
      this.windUserMapper.deleteWindUser(id);
    }
  }
  
  /**
   * 从redis缓存中取出用户数据
   *
   * @param token token令牌
   * @return 返回的用户数据
   */
  @Override
  public WindUser getTempUserInfoByToken(String token) {
    return (WindUser) this.redisTemplate.opsForValue().get(token);
  }
  
  /**
   * 缓存一个用户对象
   *
   * @param token    token令牌
   * @param windUser 要缓存得用户
   */
  @Override
  public void saveTempUserInfoByToken(String token, WindUser windUser) {
    this.redisTemplate.opsForValue().set(token, windUser, 30, TimeUnit.DAYS);
  }
  
  /**
   * 取出某个用户绑定得登录账号
   *
   * @param userId 用户得userId
   * @return 绑定得账号列表
   */
  @Override
  public List<WindAccount> getAccountsFromUserId(String userId) {
    return this.windUserMapper.getBoundAccounts(userId);
  }
  
  /**
   * 激活某用户或还原某被逻辑删除的用户
   *
   * @param id 用户的id
   */
  @Override
  public void activeWindUser(String id) throws Exception {
    // 先从数据库中查询用户的状态
    var findWindUser = this.windUserMapper.selectById(id);
    if (null == findWindUser) {
      throw new UserNotExistException("您要激活或者还原的用户不存在!");
    }
    if (DataStatus.Normal == findWindUser.getUserStatus()) {
      throw new Exception("该用户已是正常状态，无需还原");
    } else {
      var updateUser = new WindUser();
      updateUser.setUserStatus(DataStatus.Normal);
      updateUser.setUserStatusText(DataStatus.Normal.getText());
      updateUser.setUserId(id);
      this.windUserMapper.changeWindUserStatus(updateUser);
    }
  }
}
